//
//  QRCodeCardViewController.swift
//  DSWeibo
//
//  Created by J on 16/4/15.
//  Copyright © 2016年 J. All rights reserved.
//  二维码生成

import UIKit
import SnapKit
class QRCodeCardViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //1.  设置标题
        
        navigationItem.title = "我的名片"
        //2. 添加图片容器
        view.addSubview(iconView)
        
        //3. 布局图片容器
//        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: view, size: CGSizeMake(200, 200))
        iconView.snp_makeConstraints { (make) in
            make.center.equalTo(view)
            make.size.equalTo(CGSizeMake(200, 200))
        }
//        iconView.backgroundColor = UIColor.orangeColor()
        //4.生成二维码
        let qrcodeImage = createQRCodeImage()
        //5.将生成的二维码添加到图片容器上
        iconView.image = qrcodeImage
        
    
    }
    
    
    private func createQRCodeImage() -> UIImage?{
        
        //1.创建滤镜
        
        let filter = CIFilter(name: "CIQRCodeGenerator")
        //2.还原滤镜的默认属性
        filter?.setDefaults()
        
        //3.设置要生成的二维码的数据
        filter?.setValue("极客江南".dataUsingEncoding(NSUTF8StringEncoding), forKey: "inputMessage")
        
        //4. 从滤镜中取出生成好的图片
      let ciImage =  filter?.outputImage
      let bgImage =   createNonInterpolatedUIImageFormCIImage(ciImage!, size: 300)//只能生成一个正方形的
       
        //5. 创建一个头像
        let icon = UIImage(named: "tableview_loading")
        
        //6. 合成图片(将二维码和头像进行合并)
      let newImage =  createImage(bgImage, iconImage: icon!)
        //7.返沪生成好的二维码图
        
        return newImage
        
    }
    
   
    
    //MARK: -懒加载
    private lazy var iconView : UIImageView =  UIImageView()
    
}



extension QRCodeCardViewController{
    
    
    ///  合成图片
    ///
    ///  - parameter bgImag:    二维码
    ///  - parameter iconImage: 头像
    private func createImage(bgImag:UIImage, iconImage: UIImage) -> UIImage{
        //1.开启图形上下文
        
        UIGraphicsBeginImageContext(bgImag.size)
        //2.绘制背景图片
        bgImag.drawInRect(CGRect(origin: CGPointZero, size: bgImag.size))
        
        //3.绘制头像
        let width: CGFloat  = 50
        let height: CGFloat = width
        let x = 0.5*(bgImag.size.width - width)
        let y = 0.5*(bgImag.size.height - height)

        iconImage.drawInRect(CGRectMake(x, y, width, height))
        
        //4.取出绘制好的图片
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        
        //5.关闭上下文
        UIGraphicsEndImageContext()
        //6. 返回合成后的图片
        return newImage
        
    }
    
    
    /**
     根据CIImage生成指定大小的高清UIImage
     
     :param: image 指定CIImage
     :param: size    指定大小
     :returns: 生成好的图片
     */
    private func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
        
        let extent: CGRect = CGRectIntegral(image.extent)
        let scale: CGFloat = min(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent))
        
        // 1.创建bitmap;
        let width = CGRectGetWidth(extent) * scale
        let height = CGRectGetHeight(extent) * scale
        let cs: CGColorSpaceRef = CGColorSpaceCreateDeviceGray()!
        let bitmapRef = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, cs, 0)!
        
        let context = CIContext(options: nil)
        let bitmapImage: CGImageRef = context.createCGImage(image, fromRect: extent)
        
        CGContextSetInterpolationQuality(bitmapRef,  CGInterpolationQuality.None)
        CGContextScaleCTM(bitmapRef, scale, scale);
        CGContextDrawImage(bitmapRef, extent, bitmapImage);
        
        // 2.保存bitmap到图片
        let scaledImage: CGImageRef = CGBitmapContextCreateImage(bitmapRef)!
        
        return UIImage(CGImage: scaledImage)
    }

}
